<template>
<el-drawer :title="drawer.title" :visible.sync="drawer.visible" direction="rtl">
    <el-form ref="roleForm" :model="ruleForm" label-width="80px" label-position="top" class="role-form">
        <el-form-item label="最大接单数">
            <el-input v-model="ruleForm.member_max" placeholder="最大接单数"></el-input>
        </el-form-item>

        <el-form-item label="接单优先级">
            <el-input v-model="ruleForm.member_weigh" placeholder="接单优先级"></el-input>
        </el-form-item>

        <el-form-item label="策划">
            <el-checkbox-group v-model="ruleForm.company">
                <el-checkbox v-for="(item, index) in companyList" :key="index" :label="item.id+''" :name="item.id + ''">{{ item.short_name }}</el-checkbox>
            </el-checkbox-group>
        </el-form-item>
        <el-form-item label="订单权限[门店纬度]"  v-if="storeList && storeList.length > 0">
            <el-checkbox-group v-model="ruleForm.store">
                <el-checkbox v-for="(item,index) in storeList" :key="index" :label="item.id+''" :name="item.id + ''">{{ item.title }}</el-checkbox>
            </el-checkbox-group>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submit">保存</el-button>
        </el-form-item>
    </el-form>
</el-drawer>
</template>

<script>
import {
    readConfigApi,
    saveConfigApi
} from "@/api/ucenter/user";

import dictionary from "@/utils/dictionary";

const ruleForm = {
    id: "",
    member_max: "",
    member_weigh: "",
    company: [],
    store:[]
};

export default {
    data() {
        return {
            drawer: {
                title: "数据权限",
                visible: false,
            },
            ruleForm: JSON.parse(JSON.stringify(ruleForm)),
        };
    },
    computed: {
        companyList() {
            const list = dictionary.getPicker("company") || [];
          //  console.log('公司列表:', list); // 调试信息
            return Array.isArray(list) ? list : []; // 确保返回一个数组
        },
        storeList() {
            const list = dictionary.getPicker("store") || [];
          //  console.log('门店列表:', list); // 调试信息
            return Array.isArray(list) ? list : []; // 确保返回一个数组
        },
    },
    methods: {
        // 显示
        showDrawer(options) {
            this.ruleForm = JSON.parse(JSON.stringify(ruleForm));
            this.ruleForm.id = options.id;
            // 显示当前
            this.drawer.visible = true;
            this.read();
        },
        read() {
            let params = {
                id: this.ruleForm.id,
            };
            readConfigApi(params).then((res) => {
                if (res.data.detail) {
                    this.ruleForm = res.data.detail;
                    console.log('读取的表单数据:', this.ruleForm); // 调试信息
                }
            });
        },
        // 提交
        submit() {
            saveConfigApi(this.ruleForm).then((res) => {
                if (res.code == 200) {
                    this.$message.success(res.msg);
                    this.drawer.visible = false;
                    this.$emit("getTableData", {});
                } else {
                    this.$message.warning(res.msg);
                }
            });
        },
    },
};
</script>

<style lang="scss" scoped>
.role-form {
    padding: 0 32px;

    .el-checkbox {
        width: 12rem;
        margin-right: 15px;
    }
}
</style>
